<!--
 * @Date: 2025-03-12 14:12:56
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2025-03-18 14:03:49
 * @Description: 任务中心
-->
<template>
  <div class="hp-task-panel">
    <div class="hp-task-panel-header" v-if="schema.showHeader">
      <div class="header-title">{{ schema.label }}</div>
      <div class="header-tools"></div>
    </div>
    <div class="hp-task-panel-body">
      <template v-if="taskState.tabList.length === 0">
        <ElEmpty description="任务信息将通过接口返回数据自动回显" :image-size="60"/>
      </template>
      <template v-else-if="taskState.tabList.length === 1">
        <div class="task-list single">
          <div class="task-list-item" v-for="(taskItem, index) in taskState.tabList[0].taskList" :key="index">
            <div class="task-item-title">
              <span>{{ taskItem.taskName }}</span>
            </div>
            <div class="task-item-types">
              <div class="item-type-item" v-for="(typeItem, index) in taskItem.taskTypeList">
                <span>{{ typeItem.wfName }}{{ typeItem.taskNum }}条</span>
              </div>
            </div>
          </div>
        </div>
      </template>
      <template v-else>
        <el-tabs class="hp-task-tabs" v-model="taskState.tabActive" >
          <el-tab-pane
            v-for="tabItem in taskState.tabList"
            :key="tabItem.type"
            :name="tabItem.type"
          >
            <template #label>
              <div class="hp-task-tab-pane__label">
                <div class="label-title">{{ tabItem.typeName }} </div>
                <div class="label-number">
                  <span class="number-prefix">( </span>
                  <span class="number-content">{{ tabItem.totalNum }}</span>
                  <span class="number-suffix"> )</span>
                </div>
              </div>
            </template>
            <div class="hp-task-tab-pane__body">
              <div class="task-list">
                <div class="task-list-item" v-for="(taskItem, index) in tabItem.taskList" :key="index">
                  <div class="task-item-title">
                    <span>{{ taskItem.taskName }}</span>
                  </div>
                  <div class="task-item-types">
                    <div class="item-type-item"
                      v-for="(typeItem, typeIndex) in taskItem.taskTypeList"
                      :key="typeIndex"
                      @click="onJumpToMenu(taskItem.routerName, typeItem)"
                    >
                      <span>{{ typeItem.wfName }}{{ typeItem.taskNum }}条</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>

          <!-- <el-tab-pane name="cbxm" >
            <template #label>
              <div class="hp-task-tab-pane__label">储备项目</div>
            </template>
            <div class="hp-task-tab-pane__body">
              <div class="task-list">
                <div class="task-list-item">
                  <div class="task-item-title">
                    <span>储备项目填报</span>
                  </div>
                  <div class="task-item-types">
                    <div class="item-type-item">
                      <span>被退回12条</span>
                    </div>
                    <div class="item-type-item">
                      未审核999条
                    </div>
                    <div class="item-type-item">
                      未审核999条
                    </div>
                  </div>
                </div>
                <div class="task-list-item">
                  <div class="task-item-title">
                    <span>下级项目审核</span>
                  </div>
                  <div class="task-item-types">
                    <div class="item-type-item">
                      <span>被退回6条</span>
                    </div>
                    <div class="item-type-item">
                      <span>未审核9条</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane name="wsxm" >
            <template #label>
              <div class="hp-task-tab-pane__label">完善项目</div>
            </template>
            <div class="hp-task-tab-pane__body">
            </div>
          </el-tab-pane> -->
        </el-tabs>
      </template>
      
    </div>
  </div>
</template>

<script setup>
import { commonProps } from '@hp/panels/cfg.js'
import { reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useTagsViewRoutes } from '@/stores/tagsViewRoutes';
import { ElMessage } from 'element-plus';

const myTask1 = [
  { type: 1, typeName: '储备项目', taskName: '储备项目填报', wfName: '未送审', wfStatus: '001', taskNum: 23, nodeId: 'node_1', routerName: 'menu-manage' },
  { type: 1, typeName: '储备项目', taskName: '储备项目填报', wfName: '被退回', wfStatus: '004', taskNum: 0, nodeId: 'node_1', routerName: 'special-reserve-apply' },
  { type: 1, typeName: '储备项目', taskName: '下级项目审核', wfName: '未审核', wfStatus: '001', taskNum: 0, nodeId: 'node_4', routerName: 'reserve-below-audit-city' },
  { type: 1, typeName: '储备项目', taskName: '下级项目审核', wfName: '被退回', wfStatus: '004', taskNum: 0, nodeId: 'node_4', routerName: 'reserve-below-audit-city' },
  { type: 2, typeName: '完善项目', taskName: '完善项目填报', wfName: '未送审', wfStatus: '001', taskNum: 10, nodeId: 'node_1', routerName: 'standby-apply-Improve' },
  { type: 2, typeName: '完善项目', taskName: '完善项目填报', wfName: '被退回', wfStatus: '004', taskNum: 23, nodeId: 'node_1', routerName: 'standby-apply-Improve' },
  { type: 2, typeName: '完善项目', taskName: '下级完善项目审核', wfName: '未审核', wfStatus: '001', taskNum: 10, nodeId: 'node_6', routerName: 'standby-apply-Improve' },
  { type: 2, typeName: '完善项目', taskName: '下级完善项目审核', wfName: '已审核', wfStatus: '002', taskNum: 23, nodeId: 'node_6', routerName: 'standby-apply-Improve' },
  { type: 3, typeName: '赛马项目', taskName: '赛马项目申报', wfName: '未送审', wfStatus: '001', taskNum: 23, nodeId: 'node_1', routerName: 'standby-apply' },
  { type: 3, typeName: '赛马项目', taskName: '赛马项目申报', wfName: '被退回', wfStatus: '004', taskNum: 23, nodeId: 'node_1', routerName: 'standby-apply' },
]

const emits = defineEmits([ 'mounted' ])
const props = defineProps({
  ...commonProps
})

const taskState = reactive({
  tabList: [],
  tabActive: 1,
  taskTypeList: [],
})

const transfTaskData = (dataList) => {
  const typeArray = []
  for (let dataItem of dataList) {
    const {
      type, nodeId, typeName, taskName, routerName, wfName, taskNum, wfStatus
    } = dataItem

    const findItem = typeArray.find(item => item.type == type)
    if (findItem) {
      let taskList = findItem.taskList
      let taskItem = taskList.find(item => item.nodeId == nodeId)
      if (taskItem) {
        taskItem.taskTypeList.push({ wfName, taskNum, wfStatus })
      } else {
        taskList.push({
          nodeId, taskName, routerName, taskTypeList: [{ wfName, taskNum, wfStatus } ],
        })
      }
      findItem.totalNum += taskNum
      if (taskNum > 0) { ++findItem.waitNum}
    } else {
      typeArray.push({
        type, typeName, totalNum: taskNum, waitNum: 0,
        taskList: [{ nodeId, taskName, routerName, taskTypeList: [{ wfName, taskNum, wfStatus }] }]
      })
    }
  }
  taskState.tabList = typeArray
  console.log('transfTaskData', { typeArray })
}


const router = useRouter()
const { tagsViewRoutes } = useTagsViewRoutes();
const onJumpToMenu = (name, wfItem) => {
  // if (props.design) return
  const route = tagsViewRoutes.find(v => v.name === name)
  console.log('onJumpToMenu', {
    tagsViewRoutes, name, wfItem, route
  })
  if (route) {
    if (!route) return
    const { name, redirect, query, meta} = route;
    if (meta?.isLink && !meta?.isIframe) {
      window.open(meta?.isLink)
    } else if (redirect) {
      router.push(redirect)
    } else {
      router.push({ name, query: { ...query } })
    }
  } else {
    let msg = `菜单未授权，请联系管理员！`
    ElMessage.warning(msg)
    console.warn(msg)
  }
}

transfTaskData(myTask1)

onMounted(() => {
  nextTick(() => { emits('mounted') })
})

</script>

<style lang="scss" scoped>

.hp-task-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
  &-header {
    height: 45px;
    display: flex;
    align-items: center;
    padding: 0 8px;
    border-bottom: 1px solid #dcdfe6;
    .header-title {
      font-weight: bold;
    }
    .header-tools {
      margin-left: auto;
    }
  }
  &-body {
    flex: 1;
    min-height: 10px;
    padding: 0;
    overflow: auto;
  }
}

.hp-task-tabs {
  :deep(.el-tabs__header) {
    margin: 0;
  }
  :deep(.el-tabs__item) {
    padding: 0 !important;
  }
  :deep(.el-tabs__content) {
    padding: 18px;
    overflow: auto;
  }
}
.hp-task-tab-pane {
  &__label  {
    display: flex;
    align-items: center;
    padding: 0 20px;
    .label-number {
      color: #f56c6c;
      margin-left: 5px;
      position: relative;
      top: -1px;
      .number-content {
        position: relative;
        top: 1px;
      }
    }
  }
  &__body {
    height: 100%;
    // display: grid;
    // gap: 12px 20px;
    // grid-template-columns: repeat(3, minmax(50px,1fr));
  }
}

.task-list {
  &.single {
    padding: 12px;
  }
  .task-list-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    align-items: center;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .task-item-title {
    font-weight: 500;
    flex: 1;
  }
  .task-item-types {
    display: flex;
    gap: 12px;
    .item-type-item {
      padding: 5px 6px;
      width: 100px;
      color: #409eff;
      background-color: #ecf5ff;
      border-radius: 5px;
      cursor: pointer;
      text-align: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      span {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }
}

</style>